package com.winapi.winapiclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.winapi.winapiclientsdk.utils.SignUtils;
import com.winapi.winapiclientsdk.model.User;

import java.util.HashMap;

/**
 * @author yyzhang
 * @version 1.0
 */
public class WinapiClient extends CommonApiClient {
    public WinapiClient(String accessKey,String secretKey){
        super(accessKey,secretKey);
    }
    public String getNameByGet(String name){
//可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("name",name);
        String result3= HttpUtil.get(GATEWAY_HOST+"/api/name/", paramMap);
        System.out.println(result3);
        return result3;
    }


    public String getNameByPost(String name){
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("name", name);

        String result= HttpUtil.post(GATEWAY_HOST+"/api/name/", paramMap);

        System.out.println(result);
        return result;
    }


    private HashMap<String,String> getMap(String body){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("accessKey",accessKey);
        //用户请求参数
        map.put("body",body);
        map.put("nonce",String.valueOf(RandomUtil.randomNumbers(4)));
        map.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
        map.put("sign", SignUtils.getSign(body,secretKey));
        return map;
    }

//    public String getUserNameByPost(User user){
//        String json = JSONUtil.toJsonStr(user);
//        HttpResponse result2 = HttpRequest.post(GATEWAY_HOST+"/api/name/user")
//                .addHeaders(getMap(json))
//                .body(json)
//                .execute();
//        System.out.println(result2.getStatus());
//        System.out.println(result2.body());
//        return "Post:用户名字是："+result2.body();
//    }
    /**
     * 获取用户名
     * @param user
     * @return
     */
    public String getUserNameByPost(User user){
        String json = JSONUtil.toJsonStr(user);
        return HttpRequest.post(GATEWAY_HOST+"/api/name/user")
                .addHeaders(getHeadMap(json,accessKey,secretKey))
                .body(json)
                .execute().body();
    }
}
